﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Models
{

    /// <summary>文本字段。</summary>
    [Serializable]
    public sealed class TextField
    {

        /// <summary>名称。</summary>
        public string Name { get; set; }

        /// <summary>值。</summary>
        public string Value { get; set; }

        /// <summary>文本字段。</summary>
        public TextField() { }

        /// <summary>创建文本字段。</summary>
        public TextField(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }

        /// <summary>创建文本字段。</summary>
        public TextField(KeyValuePair<string, string> pair)
        {
            this.Name = pair.Key;
            this.Value = pair.Value;
        }

        /// <summary></summary>
        public override string ToString() => $"{Name} = {Value}";

        /// <summary></summary>
        public override bool Equals(object obj)
        {
            if (obj != null && obj is TextField field) return field.Name == Name && field.Value == Value;
            return false;
        }

        /// <summary></summary>
        public override int GetHashCode() => (Name ?? "").GetHashCode() ^ (Value ?? "").GetHashCode();

        /// <summary></summary>
        public static implicit operator KeyValuePair<string, string>(TextField field) => field == null ? default : new KeyValuePair<string, string>(field.Name, field.Value);

        /// <summary></summary>
        public static implicit operator TextField(KeyValuePair<string, string> pair) => new KeyValuePair<string, string>(pair.Key, pair.Value);

    }

}
